package com.dongge.dao;

import com.dongge.entity.Article;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.util.List;

/**
 * Created by Administrator on 2015/10/17.
 */
public class ArticleDao {

    private SessionFactory sessionFactory;


    protected Session getSession(){
        return  sessionFactory.getCurrentSession();
    }

    /**
     * 增
     * @param model
     */
    public void save(Article model){
        if(model!=null)
        {
            getSession().save(model);
        }
    }

    /**
     * 改
     * @param model
     */
    public void update(Article model){
        if(model!=null){
            getSession().update(model);
        }
    }

    /**
     * 查一个
     * @param id
     */
    public Article get(Long id){
        if(id != null){
            return (Article)getSession().get(Article.class, id);
        }
        return  null;
    }

    /**
     * 查询所有
     * @return
     */
    public List<Article> getAll(){
        String hsql = "from Article";
        return getSession().createQuery(hsql).list();
    }

    /**
     * 删
     * @param id
     */
    public void delete(Long id){
        Article model = get(id);
        if(model != null)
        {
            getSession().delete(model);
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
